<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        let arr = [6, 8, 4, 7];

        function solve(arr){
            let queue = [];
            queue.push(arr[0]);
            for(let i = 1; i < arr.length; i++){
                if(queue.length < 2){
                    if(arr[i] == queue[0])
                        continue;
                    queue.push(0);
                    if(arr[i] < queue[0])
                        queue[1] = arr[i];
                    else{
                        queue[1] = queue[0];
                        queue[0] = arr[i];
                    }
                    continue;
                }
                if(arr[i] > queue[0]){
                    queue[1] = queue[0];
                    queue[0] = arr[i]
                }
                else if(arr[i] > queue[1])
                    queue[1] = arr[i];
            }
            console.log(queue[1]);
        }
        solve(arr);
    </script>
</body>
</html>